package stark.app.function.articlegen;

import android.text.TextUtils;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.articlegen.lib.ArticleGenModule;
import com.stark.articlegen.lib.QuotationUtil;
import com.stark.articlegen.lib.bean.QuotationsCategory;
import com.stark.articlegen.lib.bean.QuotationsSentence;
import com.stark.articlegen.lib.constant.QuotationsCateType;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import stark.app.R;
import stark.app.databinding.ActivityArticleGenDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.retrofit.INewReqRetCallback;

/**
 * Author: daizhongbin on 2022/9/29
 * Email: 1027154338@qq.com
 * Desc:
 */
public class ArticleGenDemoActivity extends BaseNoModelActivity<ActivityArticleGenDemoBinding> {

    @Override
    protected int onCreate() {
        return R.layout.activity_article_gen_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnGen.setOnClickListener(v -> {
            genArticle();
        });
        mDataBinding.btnQuotationsCate.setOnClickListener(v -> {
            getQuotationsCate();
        });
    }

    @Override
    protected void initData() {

    }

    private void genArticle() {
        String theme = mDataBinding.etTheme.getText().toString();
        if (TextUtils.isEmpty(theme)) {
            ToastUtils.showShort("请输入文章主题");
            mDataBinding.etTheme.requestFocus();
            return;
        }
        String strCount = mDataBinding.etCount.getText().toString();
        if (TextUtils.isEmpty(strCount)) {
            ToastUtils.showShort("请输入字符个数");
            mDataBinding.etCount.requestFocus();
            return;
        }
        int wordCount = Integer.parseInt(strCount);
        if (wordCount <= 0 || wordCount > 10000) {
            ToastUtils.showShort("字符个数必须大于0并且小于10000");
            mDataBinding.etCount.requestFocus();
            return;
        }
        ArticleGenModule.articleGenApi().genArticle(this, theme, wordCount, new INewReqRetCallback<String>() {
            @Override
            public void onResult(int code, String msg, @Nullable String result) {
                ToastUtils.showShort(msg);
                mDataBinding.tvArticle.setText(result);
            }
        });
    }

    private StringBuilder strBuilder;
    private void getQuotationsCate() {
        int page = 1;
        int pageSize = 100;
        strBuilder = new StringBuilder();
        showDialog("加载数据中...");
        ArticleGenModule.quotationsApi().getCategories(this, page, pageSize, new INewReqRetCallback<List<QuotationsCategory>>() {
            @Override
            public void onResult(int code, String msg, @Nullable List<QuotationsCategory> result) {
                ToastUtils.showShort(msg);
                if (result != null && result.size() > 0) {
                    strBuilder.append("获取到的语录分类如下：\n");
                    strBuilder.append(GsonUtils.toJson(result) + "\n\n");

                    //获取到的分类中的img_url为空，或者自己要给分类配置图片的话，可将图片放到asset目录下，然后通过下面方法处理，看下面注释代码例子
                    //Map<Integer, String> cateImgAssetPathMap = new HashMap<>();
                    //cateImgAssetPathMap.put(QuotationsCateType.DUI_REN.getId(), "怼人分类图片在asset下的路径");
                    //cateImgAssetPathMap.put(QuotationsCateType.FA_FENG_HEI_HUA.getId(), "发疯/黑化文学分类图片在asset下的路径");
                    //...
                    //QuotationUtil.handleCategoryImgUrl(result, cateImgAssetPathMap);

                    //获取到分类后，获取某一分类下的句子
                    getQuotationsSentence(result.get(0));
                }
            }
        });
    }

    private void getQuotationsSentence(QuotationsCategory category) {
        int page = 1;
        int pageSize = 100;
        ArticleGenModule.quotationsApi().getSentencesByCategory(this, category.id, page, pageSize, new INewReqRetCallback<List<QuotationsSentence>>() {
            @Override
            public void onResult(int code, String msg, @Nullable List<QuotationsSentence> result) {
                dismissDialog();
                if (result != null && result.size() > 0) {
                    strBuilder.append("获取到[" + category.name + "]分类下的句子为：\n");
                    strBuilder.append(GsonUtils.toJson(result));
                }
                mDataBinding.tvArticle.setText(strBuilder.toString());
            }
        });
    }
}
